package com.huajx.core.handler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.huajx.util.StrUtil;

public interface ConstantHttp {
	String css = ".css";
	String png = ".png";
	String jpeg = ".jpeg";
	String jpg = ".jpg";
	String gif = ".gif";
	String svg = ".svg";
	String ico = ".ico";
	String js = ".js";
	String json = ".json";
	String woff2 = ".woff2";
	String woff = ".woff";
	String ttf = ".ttf";
	String html = ".html";
	String mp3 = ".mp3";
	String docx = ".docx";

	String context_type_css = "text/css; charset=UTF-8";
	String context_type_png = "image/png";
	String context_type_jpeg = "image/jpeg";
	String context_type_jpg = "image/jpeg";
	String context_type_gif = "image/gif";
	String context_type_ico = "image/x-icon";
	String context_type_svg = "image/svg+xml";
	String context_type_js = "application/x-javascript";
	String context_type_json = "application/json; charset=UTF-8";
	String context_type_woff2 = "application/woff2";
	String context_type_woff = "application/woff";
	String context_type_ttf = "application/ttf";
	String context_type_html = "text/html; charset=UTF-8";
	String context_type_mp3 = "audio/mpeg";
	String context_type_docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
	Map<String, String> context_types = new ConcurrentHashMap<>();
	
	static String get(String url){
		if(StrUtil.isBlank(url) || !url.contains(StrUtil.DOT)){
			return null;
		}
		
		String key = url.substring(url.lastIndexOf(StrUtil.DOT));
		
		String type = context_types.get(key);
		if(StrUtil.isNotBlank(type)){
			return type;
		}
		
		if(url.endsWith(css)){
			type = context_type_css;
			context_types.put(key, type);
		} else if(url.endsWith(js)){
			type = context_type_js;
		} else if(url.endsWith(json)){
			type = context_type_json;
			context_types.put(key, type);
			context_types.put(key, type);
		} else if(url.endsWith(html)){
			type = context_type_html;
			context_types.put(key, type);
		} else if(url.endsWith(png)){
			type = context_type_png;
			context_types.put(key, type);
		} else if(url.endsWith(jpg)){
			type = context_type_jpg;
			context_types.put(key, type);
		} else if(url.endsWith(jpeg)){
			type = context_type_jpeg;
			context_types.put(key, type);
		} else if(url.endsWith(gif)){
			type = context_type_gif;
			context_types.put(key, type);
		} else if(url.endsWith(ico)){
			type = context_type_ico;
			context_types.put(key, type);
		} else if(url.endsWith(svg)){
			type = context_type_svg;
			context_types.put(key, type);
		} else if(url.endsWith(mp3)){
			type = context_type_mp3;
			context_types.put(key, type);
		} else if(url.endsWith(woff2)){
			type = context_type_woff2;
			context_types.put(key, type);
		} else if(url.endsWith(woff)){
			type = context_type_woff;
			context_types.put(key, type);
		} else if(url.endsWith(ttf)){
			type = context_type_ttf;
			context_types.put(key, type);
		} else if(url.endsWith(docx)){
			type = context_type_docx;
			context_types.put(key, type);
		}
		return type;
	}
}
